package com.hiyee.api;
import com.hiyee.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基础控制器类
 *
 * @author WangBin
 */
@RestController
public class BaseController {

    @Autowired
    public RedisOperator redisOperator;

    // 手机短信验证码
    public static final String REDIS_KEY_MOBILE_SMS_CODE = "hiyee:mobile:sms_code:";
    // 手机短信验证码 60 秒限制
    public static final String REDIS_KEY_MOBILE_SMS_CODE_LOCK = "hiyee:mobile:sms_code:lock";
    // 用户 token
    public static final String REDIS_KEY_USER_TOKEN = "hiyee:user:token:";
    // 用户信息
    public static final String REDIS_KEY_USER_INFO = "hiyee:user:info:";
    // 管理员 token
    public static final String REDIS_KEY_ADMIN_TOKEN = "hiyee:admin:token:";
    // 管理员信息
    public static final String REDIS_KEY_ADMIN_INFO = "hiyee:admin:info";

    // 默认页数
    public static final Integer DEFAULT_PAGE = 1;
    // 默认显示记录数
    public static final Integer DEFAULT_PAGE_SIZE = 20;

    // domain 配置
    @Value("${website.domain-name}")
    public String WEBSITE_DOMAIN_NAME;

    // 浏览器 cookie 有效期
    public static final Integer COOKIE_LIFE_TIME = 12 * 60 * 60;

    /**
     * 获取参数验证错误信息
     *
     * @param bindingResult
     * @return
     */
    public Map<String, String> getErrors(BindingResult bindingResult)
    {
        Map<String, String> map = new HashMap<>();
        List<FieldError> errorList = bindingResult.getFieldErrors();
        for (FieldError error : errorList) {
            map.put(error.getField(), error.getDefaultMessage());
        }
        return map;
    }

    /**
     * 配置 cookie
     *
     * @param request
     * @param response
     * @param cookieName
     * @param cookieValue
     * @param lifetime
     */
    public void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, Integer lifetime)
    {
        try {
            cookieValue = URLEncoder.encode(cookieValue, "UTF-8");
            setCookieValue(request, response, cookieName, cookieValue, lifetime);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除 cookie
     *
     * @param request
     * @param response
     * @param cookieName
     */
    public void deleteCookie(HttpServletRequest request, HttpServletResponse response, String cookieName)
    {
        try {
            String cookieValue = URLEncoder.encode("", "UTF-8");
            setCookieValue(request, response, cookieName, cookieValue, 0);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置浏览器 cookie
     *
     * @param request
     * @param response
     * @param cookieName
     * @param cookieValue
     * @param lifetime
     */
    public void setCookieValue(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, Integer lifetime) {
        Cookie cookie = new Cookie(cookieName, cookieValue);
        cookie.setMaxAge(lifetime);
        cookie.setDomain(WEBSITE_DOMAIN_NAME);
        cookie.setPath("/");
        response.addCookie(cookie);
    }
}
